//
//  WZMChatViewController.m
//  WZMChat
//
//  Created by WangZhaomeng on 2018/9/4.
//  Copyright © 2018年 WangZhaomeng. All rights reserved.
//

#import "WZMChatViewController.h"
//#import "WZMChatSystemCell.h"
//#import "WZMChatTextMessageCell.h"
//#import "WZMChatVoiceMessageCell.h"
//#import "WZMChatImageMessageCell.h"
//#import "WZMChatVideoMessageCell.h"
#import "XMWebSocket.h"

#import "XMChatTextMessageOthersTableViewCell.h"
#import "XMChatTextMessageTableViewCell.h"
#import "XMChatImageMessageOthersTableViewCell.h"
#import "XMChatImageMessageTableViewCell.h"
#import "XMChatVoiceMessageOthersTableViewCell.h"
#import "XMChatVoiceMessageTableViewCell.h"
#import "XMChatSystemTableViewCell.h"
#import "Utils.h"


@interface WZMChatViewController ()<UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate,WZMInputViewDelegate,XMWebSocketDelegate>

///列表相关
@property (nonatomic, assign) CGFloat tableViewY;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *messageModels;
///当前私聊用户
@property (nonatomic, strong) WZMChatUserModel *userModel;
///当前群聊
@property (nonatomic, strong) WZMChatGroupModel *groupModel;
///私聊消息是否显示昵称, 不需要手动设置, 会根据私聊或者群聊用户自动判断, 此处只做记录
@property (nonatomic, assign, getter=isShowName) BOOL showName;
///自定义表情键盘
@property (nonatomic, strong) WZMInputView *myInputView;
///手势处理相关
@property (nonatomic, weak) id<UIGestureRecognizerDelegate> recognizerDelegate;
@property (nonatomic, assign, getter=isDeferredSystemGestures) BOOL deferredSystemGestures;
///下拉加载
//此次加载的消息数量,为了计算加载后消息列表的偏移量
@property (nonatomic, assign) NSInteger loadCount;
@property (nonatomic, assign, getter=isLoading) BOOL loading;
@property (nonatomic, strong) UIActivityIndicatorView *loadingView;
@property (nonatomic, strong) XMWebSocket *socket;
@end

#define WZM_LOADING_HEADER 50.0
@implementation WZMChatViewController

- (instancetype)initWithUser:(WZMChatUserModel *)userModel {
    self = [super init];
    if (self) {
        [self setConfig:userModel];
    }
    return self;
}

- (instancetype)initWithGroup:(WZMChatGroupModel *)groupModel {
    self = [super init];
    if (self) {
        [self setConfig:groupModel];
    }
    return self;
}

- (instancetype)initWithSession:(WZMChatSessionModel *)sessionModel {
    self = [super init];
    if (self) {
        [self setConfig:[[WZMChatDBManager DBManager] selectChatModel:sessionModel]];
    }
    return self;
}

- (void)setConfig:(WZMChatBaseModel *)model {
    NSString *uid = @"00002";
    WZMChatUserModel *tempModel = [[WZMChatUserModel alloc] init];
    tempModel.uid = uid;
    tempModel.name = [NSString stringWithFormat:@"打枪的不要:%@",uid];
    tempModel.avatar = @"http://sqb.wowozhe.com/images/touxiangs/10026820.jpg";
    tempModel.showName = YES;
    model = tempModel;
    
    self.navigationItem.title = @"消息";
    self.tableViewY = CHAT_NAV_BAR_H;
    if ([model isKindOfClass:[WZMChatUserModel class]]) {
        self.userModel = tempModel;
        self.showName = self.userModel.isShowName;
    }
    else {
        self.groupModel = (WZMChatGroupModel *)model;
        self.showName = self.groupModel.isShowName;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.socket = [[XMWebSocket alloc] initWithServerIp:nil];
    self.socket.delegate = self;
    [self.socket connectWebSocket];
    
    //UI布局
    [self createViews];
    //屏蔽系统底部手势
    self.deferredSystemGestures = YES;
    //加载聊天记录
    [self loadMessage:0];
    //模拟发送消息
    [self setRightItem];
}

- (void)createViews {
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.myInputView];
    [self.tableView addSubview:self.loadingView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //    [self updateRecognizerDelegate:YES];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    //    [self updateRecognizerDelegate:NO];
    self.deferredSystemGestures = NO;
}

//从数据库加载聊天记录
- (void)loadMessage:(NSInteger)page {
    [self beginLoading:page];
    NSMutableArray *messages;
    if (self.userModel) {
        messages = [[WZMChatDBManager DBManager] messagesWithUser:self.userModel page:page];
    } else {
        messages = [[WZMChatDBManager DBManager] messagesWithGroup:self.groupModel page:page];
    }
    if (page == 0) {
        self.messageModels = messages;
        [self endLoading:page];
    } else {
        self.loadCount = messages.count;
        [messages addObjectsFromArray:self.messageModels];
        self.messageModels = messages;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self endLoading:page];
        });
    }
}

#pragma mark - 模拟收到消息

- (void)xmWebSocketDidReceiveMessage:(NSString *)message {
    if ([message isKindOfClass:[NSString class]]) {
        NSDictionary *dict = [Utils jsonStringConvertDictionary:message];
        WZMChatUserModel *model = [[WZMChatUserModel alloc] init];
        model.uid = self.userModel.uid;
        model.name = dict[@"fromUserId"];
        WZMChatMessageModel *messageModel = [WZMChatMessageManager createTextMessage:self.userModel message:dict[@"message"] isSender:NO];
        [self receiveMessageModel:messageModel];
    }
    //    NSLog(message);
}

- (void)setRightItem {
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"模拟收到消息" style:UIBarButtonItemStylePlain target:self action:@selector(rightItemClick)];
    self.navigationItem.rightBarButtonItem = item;
}

- (void)rightItemClick {
    static NSInteger msgType = 1;
    if (msgType == WZMMessageTypeSystem) {
        NSString *timeMessage = [WZMChatHelper timeFromDate:[NSDate date]];
        WZMChatMessageModel *model = [WZMChatMessageManager createSystemMessage:self.userModel
                                                                        message:timeMessage
                                                                       isSender:YES];
        [self receiveMessageModel:model];
    }
    else if (msgType == WZMMessageTypeText) {
        WZMChatMessageModel *model = [WZMChatMessageManager createTextMessage:self.userModel
                                                                      message:@"我收到了一条文本消息 看看多行显示是什么样的 哈哈哈哈哈哈哈哈哈哈哈哈哈"
                                                                     isSender:NO];
        [self receiveMessageModel:model];
    }
    else if (msgType == WZMMessageTypeImage) {
        //收到图片
        //原图和缩略图链接
        NSString *original = @"http://www.vasueyun.cn/llgit/WZMChat/2.jpg";
        NSString *thumbnail = @"http://www.vasueyun.cn/llgit/WZMChat/2_t.jpg";
        
        //图片下载的代码就不多写, 这里默认下载完成
        //原图
        UIImage *oriImage = [UIImage imageNamed:@"2.jpg"];
        //缩略图, 消息展示, 优化消息滑动时的卡顿
        UIImage *thumImage = [UIImage imageNamed:@"2_t.jpg"];
        
        //创建图片model
        WZMChatMessageModel *model = [WZMChatMessageManager createImageMessage:self.userModel
                                                                     thumbnail:thumbnail
                                                                      original:original
                                                                     thumImage:thumImage
                                                                      oriImage:oriImage
                                                                      isSender:NO];
        [self receiveMessageModel:model];
    }
    else if (msgType == WZMMessageTypeVoice) {
        //接收到声音
        //声音地址
        NSString *voiceUrl = @"";
        
        //创建录音model
        NSInteger duration = arc4random()%60+1;
        WZMChatMessageModel *model = [WZMChatMessageManager createVoiceMessage:self.userModel
                                                                      duration:duration
                                                                      voiceUrl:voiceUrl
                                                                      isSender:NO];
        [self sendMessageModel:model];
    }
    else if (msgType == WZMMessageTypeVideo) {
        //收到视频
        NSString *videoUrl = @"";
        //封面图链接
        NSString *coverUrl = @"http://www.vasueyun.cn/llgit/WZMChat/1_t.jpg";
        //下载封面图
        UIImage *coverImage = [UIImage imageNamed:@"1_t.jpg"];
        //创建视频model
        WZMChatMessageModel *model = [WZMChatMessageManager createVideoMessage:self.userModel
                                                                      videoUrl:videoUrl
                                                                      coverUrl:coverUrl
                                                                    coverImage:coverImage
                                                                      isSender:NO];
        [self sendMessageModel:model];
    }
    msgType = (msgType+1)%5;
}

#pragma mark - 发送消息
//文本消息
- (void)inputView:(WZMInputView *)inputView sendMessage:(NSString *)message {
    //清空草稿
    [[WZMChatDBManager DBManager] removeDraftWithModel:self.userModel];
    WZMChatMessageModel *model = [WZMChatMessageManager createTextMessage:self.userModel
                                                                  message:message
                                                                 isSender:YES];
    [self sendMessageModel:model];
    //    NSDictionary *dict = @{
    //        @"fromUserName":@"",
    //        @"type":@"",
    //        @"message":@"iOS测试消息",
    //        @"fromUserId":@"4444",
    //        @"toUserId":@"222",
    //        @"rocketChatToken":@"",
    //        @"fromRocketChatUserId":@"",
    //        @"toRocketChatRid":@"",
    //        @"deviceToken":@""
    //    };
    //    [self.socket sendMsg:[Utils dictionaryToJsonString:dict]];
}


//其他自定义消息, 如: 图片、视频、位置等等
- (void)inputView:(WZMInputView *)inputView didSelectMoreType:(WZInputMoreType)type {
    if (type == WZInputMoreTypeImage) {
        //发送图片
        //选择图片的代码就不多写了, 这里假定已经选择了图片
        
        //原图,
        UIImage *oriImage = [UIImage imageNamed:@"1.jpg"];
        
        //缩略图, 消息展示, 优化消息滑动时的卡顿
        //将原图按照一定的算法压缩处理成缩略图, 这里直接使用外部生成的缩略图,
        UIImage *thumImage = [UIImage imageNamed:@"1_t.jpg"];
        
        //将图片上传到服务器, 图片消息只是把图片的链接发送过去, 接收端根据链接展示图片
        //上传图片的代码就不多写, 具体上传方式根据自身服务器api决定, 这里假定图片已经上传到服务器上了, 并且返回了两个链接, 原图和缩略图
        //原图和缩略图链接
        NSString *original = @"http://www.vasueyun.cn/llgit/WZMChat/1.jpg";
        NSString *thumbnail = @"http://www.vasueyun.cn/llgit/WZMChat/1_t.jpg";
        
        //创建图片model
        WZMChatMessageModel *model = [WZMChatMessageManager createImageMessage:self.userModel
                                                                     thumbnail:thumbnail
                                                                      original:original
                                                                     thumImage:thumImage
                                                                      oriImage:oriImage
                                                                      isSender:YES];
        [self sendMessageModel:model];
    }
    else if (type == WZInputMoreTypeVideo) {
        //发送视频
        //选择视频的代码就不多写了, 这里假定已经选择了视频
        //上传到服务器, 获取视频链接
        NSString *videoUrl = @"";
        
        //封面图
        UIImage *coverImage = [UIImage imageNamed:@"2_t.jpg"];
        
        //将封面图上传到服务器, 获取封面图链接
        NSString *coverUrl = @"http://www.vasueyun.cn/llgit/WZMChat/2_t.jpg";
        
        //创建视频model
        WZMChatMessageModel *model = [WZMChatMessageManager createVideoMessage:self.userModel
                                                                      videoUrl:videoUrl
                                                                      coverUrl:coverUrl
                                                                    coverImage:coverImage
                                                                      isSender:YES];
        [self sendMessageModel:model];
    }
    else if (type == WZInputMoreTypeLocation) {
        //发送定位 - 未实现
        
    }
    else if (type == WZInputMoreTypeTransfer) {
        //文件互传 - 未实现
        
    }
}

//文本变化
- (void)inputView:(WZMInputView *)inputView didChangeText:(NSString *)text {
    //保存草稿
    [[WZMChatDBManager DBManager] setDraft:text model:self.userModel];
}

//录音状态变化
- (void)inputView:(WZMInputView *)inputView didChangeRecordType:(WZMRecordType)type {
    static NSInteger start;
    if (type == WZMRecordTypeBegin) {
        //开始录音
        start = [WZMChatHelper nowTimestamp];
    }
    else if (type == WZMRecordTypeFinish) {
        NSInteger duration = [WZMChatHelper nowTimestamp]-start;
        //结束录音
        //将录音上传到服务器, 获取录音链接
        NSString *voiceUrl = @"";
        //创建录音model
        WZMChatMessageModel *model = [WZMChatMessageManager createVoiceMessage:self.userModel
                                                                      duration:duration/1000
                                                                      voiceUrl:voiceUrl
                                                                      isSender:YES];
        [self sendMessageModel:model];
    }
    else {
        
    }
}

//键盘状态变化
- (void)inputView:(WZMInputView *)inputView willChangeFrameWithDuration:(CGFloat)duration {
    [self tableViewScrollToBottom:YES duration:duration];
}

#pragma mark - private method
//发送消息
- (void)sendMessageModel:(WZMChatMessageModel *)model {
    [self addMessageModel:model];
    
    //模拟消息发送中、发送成功、发送失败
    //根据需要可以将消息默认值设置为发送成功, 此处是为了演示效果
    NSInteger i = arc4random()%2;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (i == 0) {
            model.sendType = WZMMessageSendTypeFailed;
        }
        else {
            model.sendType = WZMMessageSendTypeSuccess;
        }
        if (self.userModel) {
            [[WZMChatDBManager DBManager] updateMessageModel:model chatWithUser:self.userModel];
        }
        else {
            [[WZMChatDBManager DBManager] updateMessageModel:model chatWithGroup:self.groupModel];
        }
        [self.tableView reloadData];
    });
    
}

//收到消息
- (void)receiveMessageModel:(WZMChatMessageModel *)model {
    [self addMessageModel:model];
}

//消息存储
- (void)addMessageModel:(WZMChatMessageModel *)model {
    [self.messageModels addObject:model];
    [_tableView reloadData];
    [self tableViewScrollToBottom:YES duration:0.25];
    if (self.userModel) {
        [[WZMChatDBManager DBManager] insertMessage:model chatWithUser:self.userModel];
    }
    else {
        [[WZMChatDBManager DBManager] insertMessage:model chatWithGroup:self.groupModel];
    }
}

- (void)tableViewScrollToBottom:(BOOL)animated duration:(CGFloat)duration {
    if (animated) {
        CGFloat keyboardH = self.myInputView.keyboardH + self.myInputView.poorTextViewH;
        CGFloat contentH = self.tableView.contentSize.height;
        CGFloat tableViewH = self.tableView.bounds.size.height;
        
        CGFloat offsetY = 0;
        if (contentH < tableViewH) {
            offsetY = MAX(contentH+keyboardH-tableViewH, 0);
        }
        else {
            offsetY = keyboardH;
        }
        CGRect TRect = self.tableView.frame;
        TRect.origin.y = self.tableViewY-offsetY;
        [UIView animateWithDuration:duration animations:^{
            self.tableView.frame = TRect;
        }];
        if (self.messageModels.count) {
            dispatch_async(dispatch_get_main_queue(), ^{
                //主线程刷新，
                [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:(self.messageModels.count-1) inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:NO];
            });
        
        }
    } else {
        if (self.messageModels.count) {
            dispatch_async(dispatch_get_main_queue(), ^{
                //主线程刷新，
                [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:(self.messageModels.count-1) inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:NO];
            });
            
        }
    }
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self.myInputView chatResignFirstResponder];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.messageModels.count;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row < self.messageModels.count) {
        WZMChatMessageModel *model = [self.messageModels objectAtIndex:indexPath.row];
        model.modelH = cell.frame.size.height;
    }
    
}

// 获取高度
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row < self.messageModels.count) {
        WZMChatMessageModel *model = [self.messageModels objectAtIndex:indexPath.row];
        if (model.modelH) return model.modelH;
    }
    return UITableViewAutomaticDimension;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    // 告诉tableView所有cell的估算高度
    self.tableView.estimatedRowHeight = 50;
    return self.tableView.rowHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row < self.messageModels.count) {
        WZMChatMessageModel *model = [self.messageModels objectAtIndex:indexPath.row];
        if (model.msgType == WZMMessageTypeSystem) {
            XMChatSystemTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XMChatSystemTableViewCell class]) forIndexPath:indexPath];
            [cell setConfig:model];
            return cell;
        }
        else if (model.msgType == WZMMessageTypeText) {
            if (model.isSender) {
                XMChatTextMessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XMChatTextMessageTableViewCell class]) forIndexPath:indexPath];
                [cell setConfig:model];
                return cell;
            }
            
            XMChatTextMessageOthersTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XMChatTextMessageOthersTableViewCell class]) forIndexPath:indexPath];
            [cell setConfig:model];
            return cell;
        } else if (model.msgType == WZMMessageTypeImage) {
            if (model.isSender) {
                XMChatImageMessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XMChatImageMessageTableViewCell class]) forIndexPath:indexPath];
                [cell setConfig:model];
                return cell;
            }
            
            XMChatImageMessageOthersTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XMChatImageMessageOthersTableViewCell class]) forIndexPath:indexPath];
            [cell setConfig:model];
            return cell;
            
        } else if (model.msgType == WZMMessageTypeVoice) {
            if (model.isSender) {
                XMChatVoiceMessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XMChatVoiceMessageTableViewCell class]) forIndexPath:indexPath];
                [cell setConfig:model];
                return cell;
            }
            
            XMChatVoiceMessageOthersTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XMChatVoiceMessageOthersTableViewCell class]) forIndexPath:indexPath];
            [cell setConfig:model];
            return cell;
            
        } else if (model.msgType == WZMMessageTypeVideo) {
            if (model.isSender) {
                XMChatImageMessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XMChatImageMessageTableViewCell class]) forIndexPath:indexPath];
                [cell setConfig:model];
                return cell;
            }
            
            XMChatImageMessageOthersTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XMChatImageMessageOthersTableViewCell class]) forIndexPath:indexPath];
            [cell setConfig:model];
            return cell;
        }
    }
    
    return [UITableViewCell new];
}

///下拉加载处理
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    CGFloat y = scrollView.contentOffset.y;
    if (y <= -WZM_LOADING_HEADER) {
        if (self.messageModels.count == 0) return;
        WZMChatMessageModel *model = self.messageModels.firstObject;
        [self loadMessage:model.timestmp];
    }
}

- (void)beginLoading:(NSInteger)page {
    if (self.isLoading) return;
    self.loading = YES;
    if (page > 0) {
        [self.loadingView startAnimating];
        self.tableView.contentInset = UIEdgeInsetsMake(WZM_LOADING_HEADER, 0.0, 0.0, 0.0);
    }
}

- (void)endLoading:(NSInteger)page {
    if (self.isLoading == NO) return;
    self.loading = NO;
    if (page == 0) {
        [self.tableView reloadData];
        [self tableViewScrollToBottom:NO duration:0.25];
    } else {
        [self.loadingView stopAnimating];
        if (self.loadCount > 0) {
            self.tableView.contentInset = UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0);
            [self.tableView reloadData];
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.loadCount inSection:0];
            [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
            CGFloat offset = self.tableView.contentOffset.y-WZM_LOADING_HEADER;
            self.tableView.contentOffset = CGPointMake(0.0, offset);
        } else {
            [UIView animateWithDuration:0.2 animations:^{
                self.tableView.contentInset = UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0);
            }];
        }
    }
}

#pragma mark - getter
- (UITableView *)tableView {
    if (_tableView == nil) {
        CGRect rect = self.view.bounds;
        rect.origin.y = self.tableViewY;
        rect.size.height -= (self.tableViewY+self.myInputView.toolViewH);
        
        _tableView = [[UITableView alloc] initWithFrame:rect];
        _tableView.delegate = self;
        _tableView.dataSource = self;
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
#else
        self.automaticallyAdjustsScrollViewInsets = NO;
#endif
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.tableFooterView = [UIView new];
        _tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
        _tableView.backgroundColor = [UIColor colorWithRed:240/255. green:240/255. blue:240/255. alpha:1];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[XMChatTextMessageOthersTableViewCell class] forCellReuseIdentifier:NSStringFromClass([XMChatTextMessageOthersTableViewCell class])];
        [_tableView registerClass:[XMChatTextMessageTableViewCell class] forCellReuseIdentifier:NSStringFromClass([XMChatTextMessageTableViewCell class])];
        [_tableView registerClass:[XMChatImageMessageOthersTableViewCell class] forCellReuseIdentifier:NSStringFromClass([XMChatImageMessageOthersTableViewCell class])];
        [_tableView registerClass:[XMChatImageMessageTableViewCell class] forCellReuseIdentifier:NSStringFromClass([XMChatImageMessageTableViewCell class])];
        [_tableView registerClass:[XMChatVoiceMessageOthersTableViewCell class] forCellReuseIdentifier:NSStringFromClass([XMChatVoiceMessageOthersTableViewCell class])];
        [_tableView registerClass:[XMChatVoiceMessageTableViewCell class] forCellReuseIdentifier:NSStringFromClass([XMChatVoiceMessageTableViewCell class])];
        [_tableView registerClass:[XMChatSystemTableViewCell class] forCellReuseIdentifier:NSStringFromClass([XMChatSystemTableViewCell class])];
        
    }
    return _tableView;
}

- (WZMInputView *)myInputView {
    if (_myInputView == nil) {
        _myInputView = [[WZMInputView alloc] init];
        _myInputView.delegate = self;
        _myInputView.text = [[WZMChatDBManager DBManager] draftWithModel:self.userModel];
    }
    return _myInputView;
}

- (NSMutableArray *)messageModels {
    if (_messageModels == nil) {
        _messageModels = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _messageModels;
}

- (UIActivityIndicatorView *)loadingView {
    if (_loadingView == nil) {
        _loadingView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        _loadingView.frame = CGRectMake(0.0, -WZM_LOADING_HEADER, CHAT_SCREEN_WIDTH, WZM_LOADING_HEADER);
        _loadingView.hidesWhenStopped = YES;
    }
    return _loadingView;
}

//#pragma mark - 录音按钮手势冲突处理
////设置手势代理
//- (void)updateRecognizerDelegate:(BOOL)appear {
//    if (appear) {
//        if (self.recognizerDelegate == nil) {
//            self.recognizerDelegate = self.navigationController.interactivePopGestureRecognizer.delegate;
//        }
//        self.navigationController.interactivePopGestureRecognizer.delegate = self;
//    }
//    else {
//        self.navigationController.interactivePopGestureRecognizer.delegate = self.recognizerDelegate;
//    }
//}
//
////是否响应触摸事件
//- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
//    if (self.navigationController.viewControllers.count <= 1) return NO;
//    if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
//        CGPoint point = [touch locationInView:gestureRecognizer.view];
//        if (point.y > CHAT_SCREEN_HEIGHT-self.myInputView.toolViewH) {
//            return NO;
//        }
//        if (point.x <= 100) {//设置手势触发区
//            return YES;
//        }
//    }
//    return NO;
//}
//
//- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
//    if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
//        CGFloat tx = [(UIPanGestureRecognizer *)gestureRecognizer translationInView:gestureRecognizer.view].x;
//        if (tx < 0) {
//            return NO;
//        }
//    }
//    return YES;
//}
//
////是否与其他手势共存，一般使用默认值(默认返回NO：不与任何手势共存)
//- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
//
//    //UIScrollView的滑动冲突
//    if ([otherGestureRecognizer.view isKindOfClass:[UIScrollView class]]) {
//
//        UIScrollView *scrollow = (UIScrollView *)otherGestureRecognizer.view;
//        if (scrollow.bounds.size.width >= scrollow.contentSize.width) {
//            return NO;
//        }
//        if (scrollow.contentOffset.x == 0) {
//            return YES;
//        }
//    }
//    return NO;
//}
//
////屏蔽屏幕底部的系统手势
//- (UIRectEdge)preferredScreenEdgesDeferringSystemGestures {
//    if (self.isDeferredSystemGestures) {
//        return  UIRectEdgeBottom;
//    }
//    self.deferredSystemGestures = YES;
//    return UIRectEdgeNone;
//}

- (void)dealloc {
    [self.socket closeWebSocket];
    NSLog(@"%@释放了",NSStringFromClass(self.class));
}

@end
